package tianrun.ziguan.api.calculate.config.service.common.edit.asset;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.apache.shiro.util.Assert;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import tianrun.ziguan.api.calculate.config.dto.request.AssetStaticPropertiesInstanceVO;
import tianrun.ziguan.api.calculate.config.dto.request.common.DeleteBody;
import tianrun.ziguan.api.calculate.config.dto.request.common.add.edit.SaveBody;
import tianrun.ziguan.api.calculate.config.dto.request.common.list.FilterField;
import tianrun.ziguan.api.calculate.config.dto.request.common.list.ListBody;
import tianrun.ziguan.api.calculate.config.entity.TAssetStaticPropertiesInstance;
import tianrun.ziguan.api.calculate.config.listener.RefreshCacheListener;
import tianrun.ziguan.api.calculate.config.service.common.CommonService;
import tianrun.ziguan.api.calculate.config.service.common.edit.AbstractDealService;
import tianrun.ziguan.api.calculate.config.service.ge.TGeTableConfigService;
import tianrun.ziguan.api.calculate.config.utils.MapUtil;
import tianrun.ziguan.api.calculate.config.service.asset.instance.ITAssetStaticPropertiesInstanceService;
import tianrun.ziguan.api.common.util.ListUtil;
import tianrun.ziguan.api.common.util.SpringContextUtil;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class AssetStaticPropertiesInstanceDealService extends AbstractDealService {

    private final ITAssetStaticPropertiesInstanceService assetStaticPropertiesInstanceService;
    private static final String moduleTableName="t_asset_static_properties_module";

    public AssetStaticPropertiesInstanceDealService(TGeTableConfigService tableConfigService, ITAssetStaticPropertiesInstanceService assetStaticPropertiesInstanceService) {
        super(tableConfigService);
        this.assetStaticPropertiesInstanceService = assetStaticPropertiesInstanceService;
    }

    @Override
    public void before(SaveBody body) {
        Long calAssetId = MapUtil.getLong(body.getRawData(), "cal_asset_id");
        String label = MapUtil.getString(body.getData(), "label");
        Assert.isTrue(Objects.nonNull(calAssetId),label+"：资产id不能为空");
        List<AssetStaticPropertiesInstanceVO> vos = assetStaticPropertiesInstanceService.listVOByCalAssetIds(Sets.newHashSet(calAssetId));
        if (vos.isEmpty())return;
        long count;
        if (Objects.equals(body.getOption(), SaveBody.Option.EDIT)) {
            count = vos.stream()
                    .filter(vo -> (!Objects.equals(vo.getId(),body.getId())) && Objects.equals(vo.getLabel(), label))
                    .count();
        } else {
            count = vos.stream()
                    .filter(vo -> Objects.equals(vo.getLabel(), label))
                    .count();
        }
        Assert.isTrue(count == 0,
                "组件中实例点位" + label + "已存在！");
    }


    @Override
    public String tableName() {
        return "t_asset_static_properties_instance";
    }

    @Override
    public void after(SaveBody body) {
        TAssetStaticPropertiesInstance instance = assetStaticPropertiesInstanceService.getById(body.getId());
        Long moduleId = instance.getModuleId();
        RefreshCacheListener.flushByModuleId(moduleId,moduleTableName);
    }

    @Override
    public void delBefore(DeleteBody body) {

    }

    @Override
    public void listAfter(List<Map<String, Object>> result) {

    }

    @Override
    @Async("asyncServiceExecutor")
    public void delAfter(DeleteBody body) {
        CommonService commonService = SpringContextUtil.getBean(CommonService.class);
        List<Map<String, Object>> list = commonService.listByIds(tableName(), body.getIds(), true);
        if (ListUtil.isEmpty(list))return;
        Set<Long> mIds = list.stream().map(j -> new JSONObject(j).toJavaObject(TAssetStaticPropertiesInstance.class)).map(TAssetStaticPropertiesInstance::getModuleId).collect(Collectors.toSet());
        mIds.forEach(mId-> RefreshCacheListener.flushByModuleId(mId,moduleTableName));

    }

    @Override
    public void infoAfter(Map<String, Object> bean) {

    }


}
